from app import db
from app.models.user_behavior import UserBehavior
from app.models.user import User
from sqlalchemy import and_


class UserInteractionService:
    """用户交互服务层（点赞 / 关注 / 拉黑）"""

    ACTION_LIKE = 'like'
    ACTION_FOLLOW = 'follow'
    ACTION_BLOCK = 'block'

    @staticmethod
    def _get_or_create(user_id: int, target_user_id: int, action: str):
        record = UserBehavior.query.filter_by(user_id=user_id, target_user_id=target_user_id, action=action).first()
        if record:
            return record
        record = UserBehavior(user_id=user_id, target_user_id=target_user_id, action=action)
        db.session.add(record)
        db.session.commit()
        return record

    @staticmethod
    def _delete(user_id: int, target_user_id: int, action: str) -> bool:
        record = UserBehavior.query.filter_by(user_id=user_id, target_user_id=target_user_id, action=action).first()
        if not record:
            return False
        db.session.delete(record)
        db.session.commit()
        return True

    # 点赞
    @staticmethod
    def like_user(user_id: int, target_user_id: int):
        return UserInteractionService._get_or_create(user_id, target_user_id, UserInteractionService.ACTION_LIKE)

    @staticmethod
    def cancel_like(user_id: int, target_user_id: int) -> bool:
        return UserInteractionService._delete(user_id, target_user_id, UserInteractionService.ACTION_LIKE)

    # 关注
    @staticmethod
    def follow_user(user_id: int, target_user_id: int):
        return UserInteractionService._get_or_create(user_id, target_user_id, UserInteractionService.ACTION_FOLLOW)

    @staticmethod
    def cancel_follow(user_id: int, target_user_id: int) -> bool:
        return UserInteractionService._delete(user_id, target_user_id, UserInteractionService.ACTION_FOLLOW)

    # 拉黑
    @staticmethod
    def block_user(user_id: int, target_user_id: int):
        return UserInteractionService._get_or_create(user_id, target_user_id, UserInteractionService.ACTION_BLOCK)

    @staticmethod
    def cancel_block(user_id: int, target_user_id: int) -> bool:
        return UserInteractionService._delete(user_id, target_user_id, UserInteractionService.ACTION_BLOCK)

    # 列表
    @staticmethod
    def get_likes(user_id: int, page: int = 1, limit: int = 10):
        query = UserBehavior.query.filter_by(user_id=user_id, action=UserInteractionService.ACTION_LIKE)
        pagination = query.order_by(UserBehavior.created_at.desc()).paginate(page=page, per_page=limit, error_out=False)
        return [r.to_dict() for r in pagination.items], pagination.total

    @staticmethod
    def get_followers(target_user_id: int, page: int = 1, limit: int = 10):
        query = UserBehavior.query.filter_by(target_user_id=target_user_id, action=UserInteractionService.ACTION_FOLLOW)
        pagination = query.order_by(UserBehavior.created_at.desc()).paginate(page=page, per_page=limit, error_out=False)
        return [r.to_dict() for r in pagination.items], pagination.total 